package com.bdmc.web.controller.alarm;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import com.bdmc.alarm.domain.vo.AlarmRuleConfigVo;
import com.bdmc.alarm.domain.bo.AlarmRuleConfigBo;
import com.bdmc.alarm.service.IAlarmRuleConfigService;
import com.bdmc.common.core.page.TableDataInfo;


/**
 * 报警规则详细配置控制器
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/alarm/config")
public class AlarmRuleConfigController extends BaseController {

    private final IAlarmRuleConfigService iAlarmRuleConfigService;

    /**
     * 查询报警规则详细配置列表
     */
    @SaCheckPermission("alarm:config:list")
    @GetMapping("/list")
    public TableDataInfo<AlarmRuleConfigVo> list(@Validated(QueryGroup.class) AlarmRuleConfigBo bo, PageQuery pageQuery) {
        return iAlarmRuleConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出报警规则详细配置列表
     */
    @SaCheckPermission("alarm:config:export")
    @Log(title = "报警规则详细配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated AlarmRuleConfigBo bo, HttpServletResponse response) {
        List<AlarmRuleConfigVo> list = iAlarmRuleConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "报警规则详细配置", AlarmRuleConfigVo.class, response);
    }

    /**
     * 获取报警规则详细配置详细信息
     */
    @SaCheckPermission("alarm:config:query")
    @GetMapping("/{ruleConfigId}")
    public AjaxResult<AlarmRuleConfigVo> getInfo(@PathVariable("ruleConfigId") Integer ruleConfigId) {
        return AjaxResult.success(iAlarmRuleConfigService.queryById(ruleConfigId));
    }

    /**
     * 新增报警规则详细配置
     */
    @SaCheckPermission("alarm:config:add")
    @Log(title = "报警规则详细配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody AlarmRuleConfigBo bo) {
        return toAjax(iAlarmRuleConfigService.insertByBo(bo));
    }

    /**
     * 修改报警规则详细配置
     */
    @SaCheckPermission("alarm:config:edit")
    @Log(title = "报警规则详细配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody AlarmRuleConfigBo bo) {
        return toAjax(iAlarmRuleConfigService.updateByBo(bo));
    }

    /**
     * 删除报警规则详细配置
     */
    @SaCheckPermission("alarm:config:remove")
    @Log(title = "报警规则详细配置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleConfigIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] ruleConfigIds) {
        return toAjax(iAlarmRuleConfigService.deleteWithValidByIds(Arrays.asList(ruleConfigIds), true));
    }
}
